/**
 *
 * Copyright (C), 2002-2017, è½¦å›½ç½‘.
 */
package main;

import java.util.List;

import org.mybatis.generator.api.ProgressCallback;
import org.mybatis.generator.codegen.AbstractJavaClientGenerator;
import org.mybatis.generator.codegen.AbstractJavaGenerator;
import org.mybatis.generator.codegen.mybatis3.IntrospectedTableMyBatis3Impl;
import org.mybatis.generator.codegen.mybatis3.javamapper.*;
import org.mybatis.generator.codegen.mybatis3.model.BaseRecordGenerator;
import org.mybatis.generator.codegen.mybatis3.model.PrimaryKeyGenerator;
import org.mybatis.generator.codegen.mybatis3.model.RecordWithBLOBsGenerator;
import org.mybatis.generator.internal.ObjectFactory;

/**
 *
 * @author zhangweifeng
 * @version $Id: MyMyBatis3Impl.java, v 0.1 2017年11月23日 下午6:43:48 zhangweifeng
 *          Exp $
 */
public class MyMyBatis3Impl extends IntrospectedTableMyBatis3Impl {
	
    protected void calculateJavaModelGenerators(List<String> warnings,
            ProgressCallback progressCallback) {
        if (getRules().generateExampleClass()) {
            AbstractJavaGenerator javaGenerator = new MyExampleGenerator();
            initializeAbstractGenerator(javaGenerator, warnings,
                    progressCallback);
            javaModelGenerators.add(javaGenerator);
        }

        if (getRules().generatePrimaryKeyClass()) {
            AbstractJavaGenerator javaGenerator = new PrimaryKeyGenerator();
            initializeAbstractGenerator(javaGenerator, warnings,
                    progressCallback);
            javaModelGenerators.add(javaGenerator);
        }

        if (getRules().generateBaseRecordClass()) {
            AbstractJavaGenerator javaGenerator = new BaseRecordGenerator();
            initializeAbstractGenerator(javaGenerator, warnings,
                    progressCallback);
            javaModelGenerators.add(javaGenerator);
        }

        if (getRules().generateRecordWithBLOBsClass()) {
            AbstractJavaGenerator javaGenerator = new RecordWithBLOBsGenerator();
            initializeAbstractGenerator(javaGenerator, warnings,
                    progressCallback);
            javaModelGenerators.add(javaGenerator);
        }
    }

	/**
	 * @return
	 * @see org.mybatis.generator.api.IntrospectedTable#calculateMyBatis3XmlMapperFileName()
	 */
	@Override
	protected String calculateMyBatis3XmlMapperFileName() {
		StringBuilder sb = new StringBuilder();
		//sb.append(fullyQualifiedTable.getDomainObjectName().replace("Model", "").replace("model", ""));
		//表名
		sb.append(fullyQualifiedTable.getIntrospectedTableName());
		sb.append("_mapper.xml");
		return sb.toString();
	}

	@Override
	protected void calculateJavaClientAttributes() {
		if (context.getJavaClientGeneratorConfiguration() == null) {
			return;
		}
		
		StringBuilder sb = new StringBuilder();
		sb.append(calculateJavaClientImplementationPackage());
		sb.append('.');
		sb.append(fullyQualifiedTable.getDomainObjectName());
		sb.append("DaoImpl"); //$NON-NLS-1$
		setDAOImplementationType(sb.toString());

		sb.setLength(0);
		sb.append(calculateJavaClientInterfacePackage());
		sb.append('.');
		sb.append(fullyQualifiedTable.getDomainObjectName());
		sb.append("Dao"); //$NON-NLS-1$
		setDAOInterfaceType(sb.toString());

		sb.setLength(0);
		sb.append(calculateJavaClientInterfacePackage());
		sb.append('.');
		sb.append(fullyQualifiedTable.getDomainObjectName().replace("DO", "").replace("Model",""));

		sb.append(context.getProperty("DAOSuffix")); //$NON-NLS-1$
		setMyBatis3JavaMapperType(sb.toString());

		sb.setLength(0);
		sb.append(calculateJavaClientInterfacePackage());
		sb.append('.');
		sb.append(fullyQualifiedTable.getDomainObjectName());
		sb.append("SqlProvider"); //$NON-NLS-1$
		setMyBatis3SqlProviderType(sb.toString());
		
//		sb.setLength(0);
//		sb.append(calculateJavaClientInterfacePackage()+".example");
//		sb.append('.');
//		sb.append(fullyQualifiedTable.getDomainObjectName());
//		sb.append("Example"); //$NON-NLS-1$
//		System.out.println(sb.toString());
	}
	
	/** 
	 * @return
	 * @see org.mybatis.generator.api.IntrospectedTable#getExampleType()
	 */
	@Override
	public String getExampleType() {
		StringBuilder sb = new StringBuilder();
		sb.setLength(0);
		//sb.append(calculateJavaClientInterfacePackage()+".example");
		sb.append(calculateJavaModelPackage()+".example");
		sb.append('.');
		sb.append(fullyQualifiedTable.getDomainObjectName().replace("DO", "").replace("Model",""));
		sb.append("Example"); //$NON-NLS-1$
		return sb.toString();
	}



	protected AbstractJavaClientGenerator createJavaClientGenerator() {
		if (context.getJavaClientGeneratorConfiguration() == null) {
			return null;
		}

		String type = context.getJavaClientGeneratorConfiguration()
				.getConfigurationType();

		AbstractJavaClientGenerator javaGenerator;
		if ("XMLMAPPER".equalsIgnoreCase(type)) { //$NON-NLS-1$
			//javaGenerator = new JavaMapperGenerator();
			javaGenerator = new MyJavaMapperGenerator();
		} else if ("MIXEDMAPPER".equalsIgnoreCase(type)) { //$NON-NLS-1$
			javaGenerator = new MixedClientGenerator();
		} else if ("ANNOTATEDMAPPER".equalsIgnoreCase(type)) { //$NON-NLS-1$
			javaGenerator = new AnnotatedClientGenerator();
		} else if ("MAPPER".equalsIgnoreCase(type)) { //$NON-NLS-1$
			javaGenerator = new JavaMapperGenerator();
		} else {
			javaGenerator = (AbstractJavaClientGenerator) ObjectFactory
					.createInternalObject(type);
		}

		return javaGenerator;
	}
}
